/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cs4280.dao;

import cs4280.bean.Message;
import cs4280.jdbc.DB;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.NamingException;
import javax.sql.DataSource;
import cs4280.bean.*;
import java.util.Iterator;

/**
 *
 * @author PangPei
 */
public class MessageDAO {

    public static enum RESULT {

        OK, DUPLICATE, FAIL
    };

    public ArrayList<Message> getMessages() {
        ArrayList<Message> bookings = new ArrayList<Message>();

        try {
            DataSource ds = DB.getDataSource();
            Connection conn = ds.getConnection();
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT [id], [email], [time], [content],[name] FROM [messagebox] ORDER BY [time] DESC;");
            while (rs != null && rs.next() != false) {
                Message message = new Message();
                message.setId(rs.getInt(1));
                message.setEmail(rs.getString(2));
                message.setTime(rs.getString(3));
                message.setContent(rs.getString(4));
                message.setName(rs.getString(5));
                bookings.add(message);
            }
            if (rs != null) {
                rs.close();
            }
            if (stmt != null) {
                stmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return bookings;
    }

    public RESULT addMessage(Message newMessage) {

        RESULT result = RESULT.FAIL;
        try {
            DataSource ds = DB.getDataSource();
            Connection conn = ds.getConnection();
            PreparedStatement pstmt;

            //there is no duplicate movie
            pstmt = conn.prepareStatement("INSERT INTO [messagebox]([email],[time],[content],[name]) VALUES(?,?,?,?)");
            pstmt.setString(1, newMessage.getEmail());
            pstmt.setString(2, newMessage.getTime());
            pstmt.setString(3, newMessage.getContent());
            pstmt.setString(4, newMessage.getName());
            int rows = pstmt.executeUpdate();

            //successfully add the movie
            if (rows > 0) {

                result = MessageDAO.RESULT.OK;
            } //insert movie fail
            else {
                result = MessageDAO.RESULT.FAIL;
            }

            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }


        } catch (NamingException ne) {

            /*ignore*/
        } catch (SQLException sqle) {

            /*ignore*/
        }
        return result;
    }

        public RESULT deleteMessage(int messageid) {

        RESULT result = RESULT.FAIL;
        try {
            DataSource ds = DB.getDataSource();
            Connection conn = ds.getConnection();
            PreparedStatement pstmt;

            //there is no duplicate movie
            pstmt = conn.prepareStatement("DELETE FROM [messagebox] WHERE id=?");
            pstmt.setInt(1, messageid);
            int rows = pstmt.executeUpdate();

            //successfully add the movie
            if (rows > 0) {

                result = MessageDAO.RESULT.OK;
            } //insert movie fail
            else {
                result = MessageDAO.RESULT.FAIL;
            }

            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }


        } catch (NamingException ne) {

            /*ignore*/
        } catch (SQLException sqle) {

            /*ignore*/
        }
        return result;
    }

}
